<?php
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Helpers/Constants.php");

require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Google_Custom_Search/Prepare/Search_Keywords_Preparer.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Google_Custom_Search/Prepare/Google_CS_API_Request.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/JSON/JSON_Decoder.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/Google_Custom_Search/Filter/Search_Results_Filter.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/JSON/JSON_Encoder.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/JSON/JSON_Pretty_Printer.php");
require_once("/home/bluecbhv/Back-End/Jcuhelpers-WebTech/JSON/New_JSON_File_Creator.php");

/***********************************************
	This class would get a list of Google
	custom search results related to the
	parameter $searchKeywords.
************************************************/
class GoogleCustomSearch {
	private $preparedSearchKeywords = NULL;
	
	private $scrapedContent_1 = NULL;
	private $scrapedContent_2 = NULL;
	private $scrapedContent_3 = NULL;
	private $scrapedContent_4 = NULL;
	private $scrapedContent_5 = NULL;
	
	private $decodedJSONData_1 = NULL;
	private $decodedJSONData_2 = NULL;
	private $decodedJSONData_3 = NULL;
	private $decodedJSONData_4 = NULL;
	private $decodedJSONData_5 = NULL;
	
	private $searchResults_1 = NULL;
	private $searchResults_2 = NULL;
	private $searchResults_3 = NULL;
	private $searchResults_4 = NULL;
	private $searchResults_5 = NULL;
	private $searchResults = NULL;
	
	private $jsonData = NULL;
	private $prettyPrintedJSON = NULL;
	private $newJSONFile = NULL;
	
	/************************************************************/
	
	function __construct($searchKeywords) {
		date_default_timezone_set("Asia/Singapore");
		
		$skpObj = new SearchKeywordsPreparer();
		$this->preparedSearchKeywords = $skpObj->prepareSearchKeywords($searchKeywords);
		
		$gcarObj = new GoogleCsApiRequest($this->preparedSearchKeywords);
		$this->scrapedContent_1 = $gcarObj->makeRequest("");
		
		$this->scrapedContent_2 = $gcarObj->makeRequest("&start=11");
		$this->scrapedContent_3 = $gcarObj->makeRequest("&start=21");
		$this->scrapedContent_4 = $gcarObj->makeRequest("&start=31");
		$this->scrapedContent_5 = $gcarObj->makeRequest("&start=41");
		
		$jdObj = new JSONDecoder();
		$this->decodedJSONData_1 = $jdObj->decodeJSONData($this->scrapedContent_1);
		$this->decodedJSONData_2 = $jdObj->decodeJSONData($this->scrapedContent_2);
		$this->decodedJSONData_3 = $jdObj->decodeJSONData($this->scrapedContent_3);
		$this->decodedJSONData_4 = $jdObj->decodeJSONData($this->scrapedContent_4);
		$this->decodedJSONData_5 = $jdObj->decodeJSONData($this->scrapedContent_5);
		
		$srfObj = new SearchResultsFilter();
		$this->searchResults_1 = $srfObj->filterSearchResults($this->decodedJSONData_1);
		$this->searchResults_2 = $srfObj->filterSearchResults($this->decodedJSONData_2);
		$this->searchResults_3 = $srfObj->filterSearchResults($this->decodedJSONData_3);
		$this->searchResults_4 = $srfObj->filterSearchResults($this->decodedJSONData_4);
		$this->searchResults_5 = $srfObj->filterSearchResults($this->decodedJSONData_5);
		
		$this->searchResults = array(
			"Date & Time Created" => date("d/M/Y - h:iA", time()),
			"Search_Results" => $this->searchResults_5
		);
		
		$jeObj = new JSONEncoder();
		$this->jsonData = $jeObj->encodePHPintoJSON($this->searchResults);
		
		$jppObj = new JSONPrettyPrinter();
		$this->prettyPrintedJSON = $jppObj->prettyPrint($this->jsonData);
		
		$this->newJSONFile = new NewJSONFileCreator($this->prettyPrintedJSON, SEARCH_RESULTS_FILEPATH);
	}
}
?>